package im.elephas.androfan.common.widget;

import im.elephas.androfan.R;
import im.elephas.androfan.activity.AndroFanActivity;
import im.elephas.androfan.activity.PersonalInfoActivity;
import im.elephas.androfan.activity.SendMessageActivity;
import im.elephas.androfan.fanfouapi.items.UserItem;
import im.elephas.androfan.util.ImageGetter;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class UserWidget extends GeneralWidget implements OnLongClickListener,
		OnClickListener {

	public UserWidget(ViewGroup parentView, AndroFanActivity mActivity) {
		super(parentView, mActivity);
		super.setWidgetView(mActivity.getLayoutInflater().inflate(
				R.layout.friendsitem, null));
		initViews();
	}

	private ImageView avatarImageView;
	private TextView nameTextView;
	private TextView locationTextView;

	private UserItem userItem;

	public UserItem getUserItem() {
		return userItem;
	}

	public void setUserItem(UserItem ui) {
		this.userItem = ui;
		nameTextView.setText(userItem.getScreenName());
		locationTextView.setText("所在地: " + userItem.getLocation());
		avatarImageView.setImageBitmap(ImageGetter.getBitMap(userItem
				.getProfileImageUrl()));
	}

	protected void initViews() {
		nameTextView = (TextView) super.getWidgetView().findViewById(
				R.id.nameTextView);
		locationTextView = (TextView) super.getWidgetView().findViewById(
				R.id.locationTextView);
		avatarImageView = (ImageView) super.getWidgetView().findViewById(
				R.id.avatarImageView);
		
		getWidgetView().setClickable(true);
		getWidgetView().setOnLongClickListener(this);
		avatarImageView.setOnClickListener(this);
	}

	public UserWidget(ViewGroup parentView, AndroFanActivity mActivity, UserItem ui) {
		this(parentView, mActivity);
		setUserItem(ui);
	}

	@Override
	public void onClick(View v) {
		showDialog();
	}

	@Override
	public boolean onLongClick(View v) {
		showDialog();
		return false;
	}

	private void showDialog() {
		Builder dialog = new AlertDialog.Builder(mActivity);
		String[] sel = { "回复", "私信", "查看资料" };
		dialog.setTitle("对@" + userItem.getScreenName());
		dialog.setItems(sel, new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent i;
				Bundle b;
				switch (which) {
				case 0:
					i = new Intent(getActivity(), SendMessageActivity.class);
					b = new Bundle();
					b.putBoolean("isreply", true);
					b.putString("atname", userItem.getName());
					b.putString("replyid", userItem.getId());
					i.putExtras(b);
					getActivity().startActivityForResult(i, 0);
					break;
				case 1:
					break;
				case 2:
					i = new Intent(getActivity(), PersonalInfoActivity.class);
					b = new Bundle();
					b.putSerializable("item", userItem);
					i.putExtras(b);
					getActivity().startActivityForResult(i, 0);
					break;
				default:
					break;
				}
			}
		});
		dialog.show();
	}
}
